package miniproj.services.data;

import java.io.*;

import miniproj.models.building.Building;
import miniproj.models.groups.Group;
import miniproj.models.groups.Promotion;
import miniproj.models.groups.Teaching;
import miniproj.models.people.Student;
import miniproj.models.people.Teacher;
import miniproj.models.reservation.Reservation;
import miniproj.models.room.Room;

/**
 * This class is able to serialize data in a data file using the personalized
 * way.
 * 
 * @author William Tassoux
 * 
 */
public class SpecialDataWriter implements DataWriter {

	private String folderPath;

	/**
	 * Instantiates a new special data writer.
	 * 
	 * @param folderPath
	 *            the folder path
	 */
	public SpecialDataWriter(String folderPath) {
		this.folderPath = folderPath;
	}

	/**
	 * Serialize the DataContext using the special data format.
	 * 
	 * @param dc
	 *            the data context to be serialized
	 * @param dataContextFileName
	 *            the data context file name
	 */
	@Override
	public void setDataContext(DataContext dc, String dataContextFileName) {
		String fileFullPath = folderPath + "/" + dataContextFileName;
		StringBuilder content = new StringBuilder();
		for (Building b : dc.getBuildings()) {
			content.append("Building;" + b.getName() + "\n");
		}
		for (Group g : dc.getGroups()) {
			content.append("Group;" + g.getId() + "\n");
		}
		for (Teacher t : dc.getTeachers()) {
			content.append("Teacher;" + t.getLastName() + ";"
					+ t.getFirstName() + "\n");
		}
		for (Group g : dc.getGroups()) {
			for (Student s : g.getStudents()) {
				content.append("Student;" + s.getLastName() + ";"
						+ s.getFirstName() + ";" + g.getId() + "\n");
			}
		}
		for (Room r : dc.getRooms()) {
			content.append("Room;" + r.getClass().getSimpleName() + ";"
					+ r.getId().getBuilding().getName() + ";"
					+ r.getId().getFloor() + ";" + r.getId().getNumRoom() + ";"
					+ r.getCapacity() + "\n");
		}
		for (Promotion p : dc.getPromotions()) {
			content.append("Promotion;" + p.toString() + ";"
					+ p.getHeadTeacher().getLastName() + ";"
					+ p.getHeadTeacher().getFirstName());
			for (Group g : p.getGroups()) {
				content.append(";" + g.getId());
			}
			content.append("\n");
		}
		for (Teaching t : dc.getTeachings()) {
			content.append("Teaching;" + t.getName() + ";"
					+ t.getHeadTeacher().getLastName() + ";"
					+ t.getHeadTeacher().getFirstName() + ";"
					+ t.getTeachers().size());
			for (Teacher teacher : t.getTeachers()) {
				content.append(";" + teacher.getLastName() + ";"
						+ teacher.getFirstName());
			}
			for (Group g : t.getStudentsGroups()) {
				content.append(";" + g.getId());
			}
			content.append("\n");
		}
		for (Reservation r : dc.getReservations()) {
			content.append("Reservation;" + r.getClient().getLastName() + ";"
					+ r.getClient().getFirstName() + ";"
					+ r.getRoom().getId().getBuilding().getName() + ";"
					+ r.getRoom().getId().getFloor() + ";"
					+ r.getRoom().getId().getNumRoom() + ";"
					+ r.getDate().getDate().getTime() + ";"
					+ r.getDate().getSchedule() + ";"
					+ r.getTeaching().getName());
			for (Group g : r.getGroups()) {
				content.append(";" + g.getId());
			}
			content.append("\n");
		}
		this.writeInFile(fileFullPath, content);
	}

	private void writeInFile(String fileFullPath, StringBuilder content) {
		File dataFile = new File(fileFullPath);

		if (!dataFile.exists()) {
			try {
				dataFile.createNewFile();
			} catch (IOException e) {
				System.err.println("Error: File " + fileFullPath
						+ " cannot be created");
			}
		}

		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter(dataFile));
			bw.write(content.toString());
			bw.flush();
		} catch (IOException e) {
			System.err.println("Input/Output Exception while saving object : "
					+ e.getMessage());
		}
	}
}
